﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FCISPlus.Client.Communicator;

namespace FCISPlus.Client.UI
{
    public partial class Messages : Form
    {
        private TimeSpan messageLifeTime = new TimeSpan(30, 0, 0, 0);
        private int messagePreviewLen = 100;
        private IFCISPlusClientCommunicator communicator;
        public Messages(IFCISPlusClientCommunicator communicator)
        {
            InitializeComponent();
            this.communicator = communicator;
        }

        private void Messages_Load(object sender, EventArgs e)
        {
            FCISPlus.Server.Database.Message[] messages=this.communicator.GetRecentConversations(this.messageLifeTime);
            var messagesList=messages.Select(x => new { 
                From = x.User.Username,
                MessageDate = x.MessageDate.ToString("dd-MMM-yyyy hh:mm"),
                MessagePreview=TrimMessage(x.MessageText),
                FromUserID=x.User.UserID});
            this.dgvRecentConversations.DataSource = messagesList.ToList();
            this.dgvRecentConversations.AutoGenerateColumns = false;
        }

        private string TrimMessage(string messageText)
        {
            if (messageText.Length > this.messagePreviewLen)
            {
                messageText = messageText.Remove(this.messagePreviewLen);
            }

            return messageText;
        }

        private void dgvRecentConversations_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgvRecentConversations.SelectedRows.Count != 0)
            {
                int FromUserID = int.Parse(this.dgvRecentConversations.SelectedRows[0].Cells["FromUserID"].Value.ToString());
                FCISPlus.Server.Database.User friend=new FCISPlus.Server.Database.User();
                friend.UserID=FromUserID;

                FCISPlus.Server.Database.Message[] messages = this.communicator.GetConversationByFriend(friend, this.messageLifeTime);
                var messagesList = messages.Select(x => new
                {
                    From = x.User.Username,
                    MessageDate = x.MessageDate.ToString("dd-MMM-yyyy hh:mm"),
                    MessageText = x.MessageText
                });
                this.dgvConversation.DataSource = messagesList.ToList();
            }
        }

        private void btnSendMessage_Click(object sender, EventArgs e)
        {
            SendMessage sendMessageForm = new SendMessage(communicator, null);
            sendMessageForm.ShowDialog();
        }

        private void btnReplyToConversation_Click(object sender, EventArgs e)
        {
            if (this.dgvRecentConversations.SelectedRows.Count != 0)
            {
                int FromUserID = int.Parse(this.dgvRecentConversations.SelectedRows[0].Cells["FromUserID"].Value.ToString());
                SendMessage sendMessageForm = new SendMessage(communicator, FromUserID);
                sendMessageForm.ShowDialog();
            }
        }

    }
}
